package org.lh.ibernate.utils.impl;

import org.lh.ibernate.config.DbPoolConn;
import org.lh.ibernate.config.DbPoolFactory;
import org.lh.ibernate.core.IDbPool;
import org.lh.ibernate.core.IResultSetHandler;
import org.lh.ibernate.core.impl.ResultSetHandler;
import org.lh.ibernate.core.tools.ParameterLink;
import org.lh.ibernate.utils.IDbUtil;

import java.sql.*;
import java.util.List;

public class DbUtil implements IDbUtil {


    //可以执行的操作包括了插入，删除，更新
    @Override
    public void executeUpdate(String sql, List<Object> paramsList) throws Exception {
        IDbPool iDbPool=  DbPoolFactory.getInstance(); //获取连接池工厂
        DbPoolConn dbpoolConn=iDbPool.getDbPoolConn();
        Connection connection=dbpoolConn.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        for(int i=0;i<paramsList.size();i++){
            preparedStatement.setObject(i+1,paramsList.get(i));
        }
        preparedStatement.executeUpdate();
        connection.close();
        preparedStatement.close();
        dbpoolConn.freeConnection();
    }

    //批量插入操作，事务统一提交
    @Override
    public void batchInsert(String sql,List<Object> paramsList,int itemSize,int times) throws Exception {
        IDbPool iDbPool=DbPoolFactory.getInstance();
        DbPoolConn dbPoolConn=iDbPool.getDbPoolConn();
        Connection connection=dbPoolConn.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        int paramListIndex=0;
        for(int t=0;t<times;t++) {
            for (int j = 0; j < itemSize; j++) {
                preparedStatement.setObject(j+1,paramsList.get(paramListIndex));
                paramListIndex++;
            }
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        connection.commit();
        connection.close();
        preparedStatement.close();
        dbPoolConn.freeConnection();
    }


    //暂时查询类型的存储过程
    @Override
    public ResultSet queryCallHandle(String callSql) throws Exception {
        IDbPool iDbPool=DbPoolFactory.getInstance();
        DbPoolConn dbPoolConn=iDbPool.getDbPoolConn();
        Connection connection=dbPoolConn.getConnection();
        CallableStatement cs=connection.prepareCall(callSql);
        ResultSet resultSet=cs.executeQuery();
        dbPoolConn.freeConnection();
        return resultSet;
    }



    //包含有相应的查询占位符（针对多个查询）
    @Override
    public ResultSet query(String sql, ParameterLink parameterLink) throws Exception {
        IDbPool iDbPool=  DbPoolFactory.getInstance(); //获取连接池工厂
        DbPoolConn dbpoolConn=iDbPool.getDbPoolConn();
        Connection connection=dbpoolConn.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet = null;
        List<Object> paramsList=parameterLink.getParamList();
        try {
            for(int i=0;i<paramsList.size();i++){
                preparedStatement.setObject(i+1,paramsList.get(i));
            }
            resultSet=preparedStatement.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
        }
        dbpoolConn.freeConnection();
        return  resultSet;
    }


    @Override
    //包含有相应的查询，没有占位符参数
    public ResultSet query(String sql) throws Exception {
        IDbPool iDbPool=  DbPoolFactory.getInstance(); //获取连接池工厂
        DbPoolConn dbpoolConn=iDbPool.getDbPoolConn();
        Connection connection=dbpoolConn.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet = null;
        resultSet=preparedStatement.executeQuery();
        dbpoolConn.freeConnection();
        return  resultSet;
    }




}
